<template>
  <div>
    <div class="goods-list" v-if="goodsList.length !== 0">
      <div
        @click="listClick(item)"
        class="goods-list-item"
        v-for="item in goodsList"
        :key="item.id"
      >
        <div class="img">
          <img :src="item.list_pic_url" alt="" />
        </div>
        <div class="main">
          <p class="title">{{ item.name }}</p>
          <p class="price">{{ item.retail_price | priceFilter }}</p>
        </div>
      </div>
    </div>
    <div v-else class="cast">
      数据库没有数据
    </div>
  </div>
</template>

<script>
export default {
  props: {
    goodsList: {
      type: Array,
      default() {
        return [];
      },
    },
  },
  filters: {
    priceFilter(val) {
      return "¥" + val;
    },
  },
  methods: {
    listClick(item) {
      let index = this.goodsList.findIndex((items) => items.id == item.id);
      console.log(this.goodsList[index].id);
      let openId = localStorage.getItem("openId");
      this.$router.push({
        path: "/detail",
        query: {
          id: this.goodsList[index].id,
          // openId,
        },
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.goods-list {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  margin: 10px 10px;
  .goods-list-item {
    width: 49%;
    margin-right: 5px;
    margin-bottom: 10px;
    background-color: #fff;
    img {
      width: 100%;
      vertical-align: middle;
    }
    .main {
      text-align: center;
      font-size: 14px;
      .title {
        padding: 5px 0;
      }
      .price {
        padding: 5px 0;
        color: #f00;
      }
    }
  }
  .goods-list-item:nth-child(2n) {
    margin-right: 0;
  }
}
.cast {
  height: 200px;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #666;
  font-size: 16px;
}
</style>